package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.context.SpringContextUtils;
import com.cn.jdia.tas.model.T_attrecord;
import com.cn.jdia.tas.module.DailySheetModule;
import com.cn.jdia.tas.service.IAttrecordService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class DailyController {

	@Resource
	private IAttrecordService service = null;

	@RequestMapping("/daily")
	public String getDailyData(HttpServletRequest request, HttpServletResponse response) throws IOException {

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String start = map.get("start");
		String end = map.get("end");

		DateTimeFormatter format = DateTimeFormat.forPattern("YYYY-MM-dd");
		DateTime edDt = DateTime.parse(end, format).plusDays(1);

		String mySt = start.replaceAll("-", "");
		String myEd = edDt.toString("YYYYMMdd");

		System.out.println(mySt);
		System.out.println(myEd);

		service=(IAttrecordService)SpringContextUtils.getContext().getBean("attrecordService");
		List<T_attrecord> attrecordsByDate = service.getAttrecordsByDate2(mySt, myEd);

		String name;
		String id;
		String date;
		String time;
		String type;
		String no;
		String machine;

		// List<CheckInfoModule> list = new ArrayList<CheckInfoModule>();

		Map<String, DailySheetModule> dmap = new HashMap<>();

		for (T_attrecord acc : attrecordsByDate) {
			// System.out.println(acc.getEmployeeid() + ", " +
			// acc.getCheckdate() + " , " + acc.getChecktime() + " , "
			// + acc.getEnrollno() + "," + acc.getChecktype() + " , " +
			// acc.getAttmachinename() + " || "
			// + acc.getEmployee().getEmployeename() + " , " +
			// acc.getEmployee().getEmployeeid());

			name = acc.getEmployee().getEmployeename();
			id = acc.getEmployeeid();
			date = acc.getCheckdate();
			time = acc.getChecktime();
			type = acc.getChecktype();
			no = acc.getEnrollno();
			machine = acc.getAttmachinename();

			// CheckInfoModule module = new CheckInfoModule();
			// module.setName(name);
			// module.setId(id);
			// module.setDate(date);
			// module.setTime(time);
			// module.setType(type);
			// module.setNo(no);
			// module.setMachine(machine);
			//
			// list.add(module);

			// 用日期加人名当做key，来判断是否同一天一个人
			String mkey = date + "" + name;

			if (!dmap.keySet().contains(mkey)) {
				DailySheetModule mo = new DailySheetModule();
				mo.setId(id);
				mo.setName(name);
				mo.setDate(date);
				mo.setBegTime(time);
				mo.setEndTime("");

				dmap.put(mkey, mo);
				
			} else {
				DailySheetModule mo = dmap.get(mkey);

				String begTime = mo.getBegTime();
				String endTime = mo.getEndTime();

				if ((endTime == "") || (endTime == null)) {
					mo.setEndTime(time);
				} else {
					if (endTime.compareTo(time) < 0) {
						mo.setEndTime(time);
					}
				}
				dmap.put(mkey, mo);
			}
		}

		String retDate = gson.toJson(dmap);
		System.out.println(retDate);

		out.write(retDate);
		out.flush();
		out.close();

		return null;
	}

}
